package cn.openea.perpartner.cms.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.cms.web.dto.AssignDto;
import cn.openea.perpartner.cms.web.entity.CmsSales;
import cn.openea.perpartner.cms.web.service.ICmsSalesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.system.util.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 销售成员表(CmsSales)表控制层
 *
 * @author makejava
 * @since 2022-08-16 14:23:04
 */
@Api(tags = "销售管理")
@Controller
@RequestMapping("sales")
public class CmsSalesController extends BaseController {

    @Autowired
    private ICmsSalesService salesService;

    /**
     * 分页查询所有数据
     * 若userId为空，则可以查看全部销售数据，否则只能查看自己管理的销售数据
     * @param userId 用户id
     * @param searchStr 查询关键字
     * @return 所有数据
     */
    @ApiOperation("销售详情")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectAll(String userId,String searchStr) {
        List<CmsSales> list = salesService.selectAll(userId,searchStr);
        return getDataTable(list);
    }


    /**
     * 新增数据
     *
     * @param sales 实体对象
     * @return 新增结果
     */
    @ApiOperation("保存销售")
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult insert(@RequestBody CmsSales sales) {
        if (ObjectUtil.isEmpty(sales.getSalesId())){
            sales.setCreateUser(UserContext.getUserId());
        }
        if (StrUtil.isBlank(sales.getOpeneaId())){
            sales.setOpeneaId(ToolsUtil.autoUserID());
        }
        sales.setUpdateUser(UserContext.getUserId());
        return salesService.saveOrUpdate(sales)?success():error();
    }


    @ApiOperation("指派助理")
    @PostMapping("/assign")
    @ResponseBody
    public AjaxResult assign(@RequestBody List<AssignDto> assignList ) {
        return salesService.assign(assignList)?success():error();
    }

    @ApiOperation("聘为助理")
    @PostMapping("/hired")
    @ResponseBody
    public AjaxResult hired(@RequestBody List<Long> idList ) {
        return salesService.hired(idList)?success():error();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("销售信息")
    @GetMapping("{id}")
    @ResponseBody
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(salesService.getById(id));
    }

}

